package com.jy.adscm.order.controller;


import com.jy.adscm.dealer.service.DealerService;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.order.bean.JyOrder;
import com.jy.adscm.order.service.JyOrderService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-01-18
 */
@RestController
@RequestMapping("/adscm/order/jyOrder")
public class JyOrderController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JyOrderService jyOrderService;



    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JyOrder jyOrder, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new  AdscmSystemException("没有token");
        }
        boolean insert = jyOrderService.insertJyOrder(jyOrder,token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new  AdscmSystemException("没有token");
        }
        boolean result = jyOrderService.deleteJyOrder(id ,token);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JyOrder jyOrder, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new  AdscmSystemException("没有token");
        }
        Boolean result = jyOrderService.updateJyOrder(jyOrder,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("signOrder/{id}")
    public SimpleCoreResponse signOrder(@PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new  AdscmSystemException("没有token");
        }
        Boolean result = jyOrderService.signOrder(id,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    @RequestMapping("selectBystate/{state}")
    public SimpleCoreResponse selectBystate( @PathVariable Integer state, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new  AdscmSystemException("没有token");
        }
        List<JyOrder> result = jyOrderService.selectBystate(state,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectById/{id}")
    public SimpleCoreResponse selectById( @PathVariable Integer id, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new  AdscmSystemException("没有token");
        }
        JyOrder jyOrder = jyOrderService.selectById(id);
        return  new SimpleCoreResponse("200","修改成功",jyOrder);
    }
}

